/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.machineryrental.converters;

import cz.muni.fi.pa165.machineryrental.dto.CustomerDTO;
import cz.muni.fi.pa165.machineryrental.entities.Address;
import cz.muni.fi.pa165.machineryrental.entities.Customer;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author David Katuscak, 359273
 */
public class CustomerDTOConverter {

    /**
     * Converts Customer entity to CustomerDTO
     */
    public static CustomerDTO toDTO(Customer entity) {
        if (entity == null) {
            throw new IllegalArgumentException("Entity can't be null.");
        }

        CustomerDTO dto = new CustomerDTO();
        return applyToDTO(dto, entity);
    }

    /**
     * Converts CustometDTO to Customer entity
     */
    public static Customer toEntity(CustomerDTO dto) {
        if (dto == null) {
            throw new IllegalArgumentException("dto can't be null.");
        }

        Customer entity = new Customer();
        entity.setId(dto.getId());
        entity.setFirstName(dto.getFirstName());
        entity.setLastName(dto.getLastName());
        entity.setEmail(dto.getEmail());
        entity.setRentals(RentalDTOConverter.toEntityList(dto.getRentals()));
        entity.setPhone(dto.getPhone());
		
		Address address = new Address();
		address.setCity(dto.getCity());
		address.setCountry(dto.getCountry());
		address.setStreet(dto.getStreet());
		address.setZipcode(dto.getZipCode());
		
        entity.setAddress(address);
        entity.setCustomerType(CustomerTypeConverter.convertToEntity(dto.getCustomerType()));
        entity.setCompanyNumber(dto.getCompanyNumber());
        entity.setVatNumber(dto.getVatNumber());

        return entity;
    }

    /**
     * Overwrites DTO with information in entity
     */
    public static CustomerDTO applyToDTO(CustomerDTO dto, Customer entity) {
        if (entity == null) {
            throw new IllegalArgumentException("Entity can't be null.");
        }

        if (dto == null) {
            throw new IllegalArgumentException("dto can't be null.");
        }

        dto.setId(entity.getId());
        dto.setFirstName(entity.getFirstName());
        dto.setLastName(entity.getLastName());
        dto.setEmail(entity.getEmail());
        
        dto.setPhone(entity.getPhone());
		
		if (entity.getAddress() != null) {
			dto.setCity(entity.getAddress().getCity());
			dto.setCountry(entity.getAddress().getCountry());
			dto.setZipCode(entity.getAddress().getZipcode());
			dto.setStreet(entity.getAddress().getStreet());
		}
        
		
        dto.setCustomerType(CustomerTypeConverter.convertToDTO(entity.getCustomerType()));
        dto.setCompanyNumber(entity.getCompanyNumber());
        dto.setVatNumber(entity.getVatNumber());

        return dto;
    }

    /**
     * Converts list of customer entities to list of customer DTOs.
     *
     * @param customers Customers to convert.
     * @return List of DTOs according to given customers.
     */
    public static List<CustomerDTO> toDTOList(List<Customer> customers) {
        if (customers == null || customers.isEmpty()) {
            return new ArrayList<CustomerDTO>();
        }

        List<CustomerDTO> result = new ArrayList<CustomerDTO>();
        for (Customer c : customers) {
            result.add(CustomerDTOConverter.toDTO(c));
        }

        return result;
    }
}
